package club.pchome.dto;

import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "接口返回结果集", description = "接口统一返回实体")
public class Result<T> implements Serializable {
	private static final long serialVersionUID = -5095032233463460119L;
	/**
	 * 响应状态码，默认ResponseCode.SUCCESS
	 */
	@ApiModelProperty(value = "响应状态码：成功响应代码=200，错误响应代码=500，未登陆响应代码=501，系统异常响应代码=502")
	private int code;
	/**
	 * 响应详情说明
	 */
	@ApiModelProperty(value = "响应详情说明")
	private String msg;
	/**
	 * 响应详情数据
	 */
	@ApiModelProperty(value = "响应详情数据")
	private T result;

	/**
	 * 200响应结果信息
	 */
	public static final String SUCCESS = "success";

	/**
	 * 失败响应结果信息
	 */
	public static final String ERROR = "error";

	/**
	 * 成功响应代码
	 */
	@ApiModelProperty(value = "成功响应代码")
	public static final int SUCCESS_CORD = 200;

	/**
	 * 错误响应代码
	 */
	@ApiModelProperty(value = "错误响应代码")
	public static final int ERROR_CORD = 500;

	/**
	 * 未登陆响应代码
	 */
	@ApiModelProperty(value = "未登陆响应代码")
	public static final int NOT_LOGIN = 501;

	/**
	 * 系统异常
	 */
	@ApiModelProperty(value = "系统异常响应代码")
	public static final int SYSTEM_ERROR_CORD = 502;

	/**
	 * 默认构造器
	 */
	public Result() {
		this.setCode(Result.SUCCESS_CORD);
	}

	/**
	 * 状态码构造器
	 */
	public Result(int responseCode) {
		setCode(responseCode);
		this.setResult(null);
	}

	/**
	 * 状态码 + 消息详情构造器
	 */
	public Result(int responseCode, String message) {
		this.setCode(responseCode);
		this.setMsg(message);
		this.setResult(null);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	/**
	 * 失败结果
	 * @param code
	 * @param result
	 * @return
	 */
	public static <T> Result<T> initFailureResult(int code, T result) {
		Result<T> jsonResult = new Result<T>(code);
		jsonResult.setResult(result);
		jsonResult.setMsg(Result.ERROR);
		return jsonResult;
	}
	
	public static <T> Result<T> initFailureResult(int code, String msg, T result) {
		Result<T> jsonResult = new Result<T>(code);
		jsonResult.setResult(result);
		jsonResult.setMsg(Result.ERROR);
		return jsonResult;
	}

	/**
	 * OK
	 * @param result
	 * @return
	 */
	public static <T> Result<T> initSuccessResult(T result) {
		Result<T> jsonResult = new Result<T>(Result.SUCCESS_CORD);
		if (null != result) {
			jsonResult.setResult(result);
			jsonResult.setMsg(Result.SUCCESS);
		}
		return jsonResult;
	}

	@Override
	public String toString() {
		return "Result [code=" + code + ", msg=" + msg + ", result=" + result + "]";
	}

}